import os
import sys
import re
import array
import common
import config

def ParseFile(FileName):
	f = open(FileName,'r');
	for line in f:
		if(line == '\n'):
			continue;
		if(line[0] == '#'):
			continue; #TODO::Come up with a better method to identify comments
		Clauses = re.split('-|\n',line);
		for clause in Clauses:
			if(clause==""):
				continue;
			SymStack = ["BEGIN"]*config.MarkovOrder;
			State = [-1]*config.MarkovOrder;
			Notes = re.split('\s+|\t',clause);
			for note in Notes:
				if(note==""):
					continue;
				if(common.NoteIndex.has_key(note) == False):
					print "Invalid Note %s found\n" %(note);
					sys.exit();
				StateIndex = common.FindMarkovMatrixIndex(State);
				if (StateIndex >= 0):
					common.MarkovMatrix[StateIndex][common.NoteIndex[note]] += 1;
					print "%d %s" %(StateIndex, SymStack), note, common.MarkovMatrix[StateIndex][common.NoteIndex[note]];
				State.append(common.NoteIndex[note]);
				SymStack.append(note);
				State.pop(0);SymStack.pop(0);
	f.close();

def WriteDistribution():
	fout = open(config.OutFile,'a');
	for state in range(0,len(common.MarkovMatrix),1):
		def f(x):
			return common.MarkovMatrix[state][common.NoteIndex[x]]!=0;
		templist = common.NoteIndex.keys();
		templist = filter(f,templist);
		if(len(templist) == 0):
			continue;
		templist = sorted(templist, key=lambda x: -common.MarkovMatrix[state][common.NoteIndex[x]]);
		fout.write(str(state));
		for note in templist:
			fout.write(' '+str(common.NoteIndex[note]));
		fout.write('\n');
	fout.close();

common.CreateNoteIndex();
common.InitMarkovMatrix();
common.GetCorpusCorpusFileList();
for FileName in common.CorpusFileList:
	print "Parsing "+config.Path+'/'+FileName;
	ParseFile(config.Path+'/'+FileName);
	print "Done";
common.PrintMarkovMatrix();
WriteDistribution();
